<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class user extends Base
{
    /**
     * 用户列表
     * @return mixed
     */
    public function index(Request $request)
    {
        $page            = $request->param('page') ?: 1;
        $limit           = $request->param('limit') ?: 10;
        $start_time      = $request->param('start_time') ?: '';
        $end_time        = $request->param('end_time') ?: '';
        $keyword         = $request->param('keyword') ?: '';
        $type            = $request->param('type') ?: '';
        $city_select     = $request->param('city') ?: '';
        $province_select = $request->param('province') ?: '';
        $where           = [];

        if ($start_time && $end_time) {
            $time1                               = strtotime($start_time);
            $time2                               = strtotime($end_time);
            $where['ad_user_info.register_time'] = ['between', "$time1,$time2"];
        }

        if ($keyword) {
            $where['ad_user_info.true_name|ad_user_info.phone'] = ['like', "%{$keyword}%"];
        }

        if ($type) {
            $where['ad_user_info.type'] = $type;
        }

        if ($city_select) {
            $where['ad_user_info.city'] = $city_select;
        }

        if ($province_select) {
            $where['ad_user_info.province'] = $province_select;
        }

        $user = Db::table('ad_user_info')
            ->where($where)
            ->alias('ui')
            ->field('ui.uid,ui.true_name ,ui.user_type,( SELECT NAME FROM ad_industry WHERE id = job_three) job, ui.phone , 
                  ( SELECT province FROM ad_provinces WHERE provinceid = ui.province) AS province ,
                  ( SELECT city FROM ad_cities WHERE cityid = ui.city) AS city,
                  ( SELECT count(1) people FROM ad_user_info WHERE fu_invitation = ui.invitation) people_num,ui.register_time')
            ->paginate($limit, false, [
                'page' => $page,
                'query' => [
                    'keyword' => $keyword,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'type' => $type,
                    'city_select' => $city_select,
                    'province_select' => $province_select,
                ]
            ]);
        //省份搜索数据
        $province = Db::table('ad_provinces')->select();
        $city     = Db::table('ad_cities')->select();


        return view('index',
            compact('user', 'province', 'city', 'keyword', 'start_time', 'end_time', 'type', 'city_select',
                'province_select'));
    }


    /**
     * @param Request $request
     * @return \think\response\Json
     * 自媒体
     */
    public function media(Request $request)
    {
        $page            = $request->param('page') ?: 1;
        $limit           = $request->param('limit') ?: 10;
        $start_time      = $request->param('start_time') ?: '';
        $end_time        = $request->param('end_time') ?: '';
        $keyword         = $request->param('keyword') ?: '';
        $city_select     = $request->param('city') ?: '';
        $province_select = $request->param('province') ?: '';
        $where           = [];
        if ($city_select) {
            $where['ad_user_info.city'] = $city_select;
        }

        if ($province_select) {
            $where['ad_user_info.province'] = $province_select;
        }


        if ($start_time && $end_time) {
            $time1                               = strtotime($start_time);
            $time2                               = strtotime($end_time);
            $where['ad_user_info.register_time'] = ['between', "$time1,$time2"];
        }

        if ($keyword) {
            $where['ad_user_info.true_name|ad_user_info.phone'] = ['like', "%{$keyword}%"];
        }

        $user = Db::table('ad_user_info')
            ->alias('ui')
            ->where($where)
            ->where(['ui.type' => 2])
            ->field('ui.uid,ui.true_name ,( SELECT NAME FROM ad_industry WHERE id = job_three) job , ui.phone ,
                  ( SELECT province FROM ad_provinces WHERE provinceid = ui.province) AS province ,
                  ( SELECT city FROM ad_cities WHERE cityid = ui.city) AS city,
                  ( SELECT sum(money) FROM ad_lcb WHERE uid = ui.uid) shouyi_money ,ui.register_time')
            ->paginate($limit, false, [
                'page' => $page,
                'query' => [
                    'keyword' => $keyword,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'city_select' => $city_select,
                    'province_select' => $province_select,
                ]
            ]);
        //省份搜索数据
        $province = Db::table('ad_provinces')->select();
        $city     = Db::table('ad_cities')->select();

        return view('media',
            compact('user', 'province', 'city', 'keyword', 'start_time', 'end_time', 'city_select', 'province_select'));
    }


    //广告主
    public function adv(Request $request)
    {
        $page            = $request->param('page') ?: 1;
        $limit           = $request->param('limit') ?: 10;
        $start_time      = $request->param('start_time') ?: '';
        $end_time        = $request->param('end_time') ?: '';
        $keyword         = $request->param('keyword') ?: '';
        $city_select     = $request->param('city') ?: '';
        $province_select = $request->param('province') ?: '';

        $where = [];
        if ($city_select) {
            $where['ad_user_info.city'] = $city_select;
        }

        if ($province_select) {
            $where['ad_user_info.province'] = $province_select;
        }


        if ($start_time && $end_time) {
            $time1                               = strtotime($start_time);
            $time2                               = strtotime($end_time);
            $where['ad_user_info.register_time'] = ['between', "$time1,$time2"];
        }

        if ($keyword) {
            $where['ad_user_info.true_name|ad_user_info.phone'] = ['like', "%{$keyword}%"];
        }

        $user = Db::table('ad_user_info')
            ->where($where)
            ->alias('ui')
            ->where(['ui.type' => 1])
            ->field('ui.uid,ui.true_name ,( SELECT NAME FROM ad_industry WHERE id = job_three) job , ui.phone , 
                  ( SELECT province FROM ad_provinces WHERE provinceid = ui.province) AS province ,
                  ( SELECT city FROM ad_cities WHERE cityid = ui.city) AS city,
                  ( SELECT sum(money) FROM ad_user_money WHERE uid = ui.uid) touru_money ,ui.register_time')
            ->paginate($limit, false, [
                'page' => $page,
                'query' => [
                    'keyword' => $keyword,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'city_select' => $city_select,
                    'province_select' => $province_select,
                ]
            ]);
        //省份搜索数据
        $province = Db::table('ad_provinces')->select();
        $city     = Db::table('ad_cities')->select();
        return view('adv',
            compact('user', 'province', 'city', 'keyword', 'start_time', 'end_time', 'city_select', 'province_select'));
    }


    /**
     * @param Request $request
     * @return \think\response\Json
     * 自媒体查看
     */
    public function other_look_media(Request $request)
    {
        $uid                 = $request->param('uid');
        $user['sum_share']   = Db::table('ad_click')->where(['uid' => $uid])->group('ad_id')->count();
        $user['valid_share'] = Db::table('ad_click_get')->where(['uid' => $uid])->count();
        $user['get_money']   = Db::query("select sum(money) as money,channel  from ad_lcb where uid = $uid group by channel");
        $user['spread_path'] = Db::table('ad_click_get')->where(['uid' => $uid])->select();
        $user['spread_path'] = Db::table('ad_click_get')->where(['uid' => $uid])->field([
            'count(id)' => 'sum_path',
            'channel'
        ])->group('channel')->select();
        return view('out_look_media', compact('user'));

    }

    /**
     * @param Request $request
     * @return \think\response\Json
     * 广告主查看
     */
    public function other_look_adv(Request $request)
    {
        $uid                 = $request->param('uid');
        $user['sum_share']   = Db::table('ad_click')->where(['uid' => $uid])->group('ad_id')->count();
        $user['valid_share'] = Db::table('ad_click_get')->where(['uid' => $uid])->count();
        $user['get_money']   = Db::query("select sum(money) as money,channel  from ad_lcb where uid = $uid group by channel");


        $user['spread_path'] = Db::table('ad_click_get')->where(['uid' => $uid])->field([
            'count(id)' => 'sum_path',
            'channel'
        ])->group('channel')->select();
        return view('out_look_adv', compact('user'));

    }


    /**
     * @return mixed
     * 用户详细
     */
    public function info(Request $request)
    {
        $page       = $request->param('page') ?: 1;
        $limit      = $request->param('limit') ?: 10;
        $invitation = $request->param('id');

        $user = Db::table('ad_user_info')
            ->alias('ui')
            ->where(['ui.uid' => $invitation])
            ->field('ui.uid,ui.true_name ,( SELECT NAME FROM ad_industry WHERE id = job_three) job , ui.phone , 
                  ( SELECT province FROM ad_provinces WHERE provinceid = ui.province) AS province ,
                  ( SELECT city FROM ad_cities WHERE cityid = ui.city) AS city,
                  ( SELECT sum(money) FROM ad_user_money WHERE uid = ui.uid) touru_money ,ui.register_time')
            ->paginate($limit, false, ['page' => $page]);

        return view('info', compact('user'));
    }

    /*代理的查看*/
    public function agent_info(Request $request)
    {
        $page       = $request->param('page') ?: 1;
        $limit      = $request->param('limit') ?: 10;
        $uid = $request->param('id');

        $user = Db::table('ad_user_agent')
                  ->alias('ug')
                  ->where(['ug.uid' => $uid])// ( SELECT sum(money) FROM ad_user_money WHERE uid = ui.uid) touru_money ,
                  ->field('ug.uid,( SELECT true_name FROM ad_user_info WHERE uid = ug.uid ) true_name , ug.mobile , 
                  ( SELECT province FROM ad_provinces WHERE provinceid = ug.province) AS province ,
                  ( SELECT city FROM ad_cities WHERE cityid = ug.city) AS city,ug.legal_person,ug.company_name,ug.address,
                 ug.id_card_num,ug.act_start_time,ug.secure_name,ug.secure_id_card_num')
                  ->paginate($limit, false, ['page' => $page]);

        return view('agent_info', compact('user'));
    }

}